package MemoryManagement.Test;

import static MemoryManagement.MemoryManagement.bitMap;
import static org.junit.jupiter.api.Assertions.*;

import MemoryManagement.*;
import org.junit.jupiter.api.*;

import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.util.concurrent.atomic.AtomicInteger;

public class MemoryManagementImplTest {

    MemoryManagementImpl memoryManager;

    @BeforeEach
    void setUp() {
        memoryManager = new MemoryManagementImpl();
        memoryManager.InitMemory(); // 初始化内存
    }

    @Test
    void allocate_validInput_allocationSuccessful() {
        // Arrange
        int PID = 1;
        int size = 1024; // 假设要分配的大小为一页

        // Act
        int result = memoryManager.Allocate(PID, size);
        int result2 = memoryManager.Allocate(PID + 1, size);

        // Assert
        assertNotEquals(-1, result); // 分配成功，返回的块号不应为-1
        System.out.println(result);
        System.out.println(result2);


        assertEquals(1, bitMap[result][0]);
        assertEquals(2, bitMap[result2][0]);

        assertEquals(4, bitMap[result][1]);
        assertEquals(4, bitMap[result2][1]);


        int cnt1 = 0;
        int cnt2 = 0;
        for (int i = 0; i < Memory.pageNums; i++) {
            if (bitMap[i][0] == PID) {
                cnt1++;
            }
            if (bitMap[i][0] == PID + 1) {
                cnt2++;
            }
        }

        assertEquals(5, cnt1);
        assertEquals(5, cnt2);


    }


    @Test
    void free_validRegister_deallocationSuccessful() {
        // Arrange
        int PID = 1;
        int size = 1024; // 假设要分配的大小为一页

        int register = memoryManager.Allocate(PID, size);

        assertEquals(1, bitMap[register][0]);
        assertEquals(1, bitMap[10][0]);
        assertEquals(1, bitMap[11][0]);
        assertEquals(1, bitMap[12][0]);
        assertEquals(1, bitMap[13][0]);

        assertEquals(-1, bitMap[10][1]);
        assertEquals(-1, bitMap[11][1]);
        assertEquals(-1, bitMap[12][1]);
        assertEquals(-1, bitMap[13][1]);

        assertTrue(Memory.PhysicBlock[10]);
        assertTrue(Memory.PhysicBlock[11]);
        assertTrue(Memory.PhysicBlock[12]);
        assertTrue(Memory.PhysicBlock[13]);

        // Act
        boolean result = memoryManager.Free(register);

        assertEquals(-1, bitMap[10][0]);
        assertEquals(-1, bitMap[11][0]);
        assertEquals(-1, bitMap[12][0]);
        assertEquals(-1, bitMap[13][0]);


        assertFalse(Memory.PhysicBlock[10]);
        assertFalse(Memory.PhysicBlock[11]);
        assertFalse(Memory.PhysicBlock[12]);
        assertFalse(Memory.PhysicBlock[13]);


    }


    @Test
    void read_validInput_dataReadSuccessfully() {
        // Arrange

        byte[] data = new byte[4]; // 假设要读取的数据为4字节


        int PID = 1;
        int size = 1024; // 假设要分配的大小为一页

        int register = memoryManager.Allocate(PID, size);
        int logicAddress = 22 << 10; // 假设逻辑地址的页号为22

        int value = 27;
        byte[] bytes1 = ByteBuffer.allocate(4).putInt(value).array();
        byte[] bytes2 = ByteBuffer.allocate(4).putInt(value).array();

        // Act
        int result = memoryManager.Write(register, logicAddress, bytes1);
        assertEquals(MemoryManagement.PAGE_FAULT, result);

        assertEquals(0, ByteBuffer.wrap(bytes1).getInt());
        memoryManager.PageFaultProcess(register, logicAddress, ByteBuffer.wrap(bytes1).getInt());

        int result2 = memoryManager.Write(register, logicAddress, bytes2);
        assertEquals(MemoryManagement.SUCCESS, result2);

        // Act
        int result3 = memoryManager.Read(register, logicAddress, data);

        // Assert
        assertEquals(MemoryManagement.SUCCESS, result3); // 读取成功

        assertEquals(27, ByteBuffer.wrap(data).getInt());
    }

    @Test
    void write_validInput_dataWrittenSuccessfully() {
        int PID = 1;
        int size = 1024; // 假设要分配的大小为一页

        int register = memoryManager.Allocate(PID, size);
        int logicAddress = 22 << 10; // 假设逻辑地址的页号为22

        int value = 27;
        byte[] bytes = ByteBuffer.allocate(4).putInt(value).array();

        // Act
        int result = memoryManager.Write(register, logicAddress, bytes);
        assertEquals(MemoryManagement.PAGE_FAULT, result);

        assertEquals(0, ByteBuffer.wrap(bytes).getInt());
        memoryManager.PageFaultProcess(register, logicAddress, ByteBuffer.wrap(bytes).getInt());

        int result2 = memoryManager.Write(register, logicAddress, bytes);
        assertEquals(MemoryManagement.SUCCESS, result2);

    }


    @Test
    void Total() {
        int PID = 27;
        int size = 1024; // 假设要分配的大小为一页
        // Act
        int register1 = memoryManager.Allocate(PID, size);

        int value = 27;
        byte[] bytes = ByteBuffer.allocate(4).putInt(value).array();

        int logicalAddress1 = (2 << 10) + 123; // 构造逻辑地址，其中页号为22，页内偏移为123
        int logicalAddress2 = (4 << 10) + 123; // 构造逻辑地址，其中页号为22，页内偏移为123
        int logicalAddress3 = (5 << 10) + 123; // 构造逻辑地址，其中页号为22，页内偏移为123
        int logicalAddress4 = (2 << 10) + 123; // 构造逻辑地址，其中页号为22，页内偏移为123
        int logicalAddress5 = (7 << 10) + 123; // 构造逻辑地址，其中页号为22，页内偏移为123
        int logicalAddress6 = (3 << 10) + 123; // 构造逻辑地址，其中页号为22，页内偏移为123
        int logicalAddress7 = (6 << 10) + 123; // 构造逻辑地址，其中页号为22，页内偏移为123
        int logicalAddress8 = (1 << 10) + 123; // 构造逻辑地址，其中页号为22，页内偏移为123
        int logicalAddress9 = (9 << 10) + 123; // 构造逻辑地址，其中页号为22，页内偏移为123
        int logicalAddress10 = (2 << 10) + 123; // 构造逻辑地址，其中页号为22，页内偏移为123
        int logicalAddress11 = (4 << 10) + 123; // 构造逻辑地址，其中页号为22，页内偏移为123

        AtomicInteger block1 = new AtomicInteger(0);
        AtomicInteger startOffset1 = new AtomicInteger(0);

        AtomicInteger block2 = new AtomicInteger(0);
        AtomicInteger startOffset2 = new AtomicInteger(0);
        int modify1 = 0;

        System.out.println("第1次");
        int result1 = memoryManager.Write(register1, logicalAddress1, bytes);
        if (result1 == MemoryManagement.PAGE_FAULT) {
            memoryManager.PageFaultProcess(register1, logicalAddress1, ByteBuffer.wrap(bytes).getInt());
            System.out.println("缺页,换出:" + ByteBuffer.wrap(bytes).getInt());
            System.out.println("当前在内存中：");
            for (int i = 10; i < Memory.pageNums; i++) {
                if (bitMap[i][0] == PID && bitMap[i][1] != -1) {
                    System.out.print(bitMap[i][1] + " ");
                }
            }
            System.out.println();
        } else {
            System.out.println("当前在内存中：");
            for (int i = 10; i < Memory.pageNums; i++) {
                if (bitMap[i][0] == PID && bitMap[i][1] != -1) {
                    System.out.print(bitMap[i][1] + " ");
                }
            }
            System.out.println();
        }
        System.out.println();


        System.out.println("第2次");
        int result2 = memoryManager.Write(register1, logicalAddress2, bytes);
        if (result2 == MemoryManagement.PAGE_FAULT) {
            memoryManager.PageFaultProcess(register1, logicalAddress2, ByteBuffer.wrap(bytes).getInt());
            System.out.println("缺页,换出:" + ByteBuffer.wrap(bytes).getInt());
            System.out.println("当前在内存中：");
            for (int i = 10; i < Memory.pageNums; i++) {
                if (bitMap[i][0] == PID && bitMap[i][1] != -1) {
                    System.out.print(bitMap[i][1]+ " ");
                }
            }
            System.out.println();
        } else {
            System.out.println("当前在内存中：");
            for (int i = 10; i < Memory.pageNums; i++) {
                if (bitMap[i][0] == PID && bitMap[i][1] != -1) {
                    System.out.print(bitMap[i][1]+ " ");
                }
            }
            System.out.println();
        }
        System.out.println();

        System.out.println("第3次");
        int result3 = memoryManager.Write(register1, logicalAddress3, bytes);
        if (result3 == MemoryManagement.PAGE_FAULT) {
            memoryManager.PageFaultProcess(register1, logicalAddress3, ByteBuffer.wrap(bytes).getInt());
            System.out.println("缺页,换出:" + ByteBuffer.wrap(bytes).getInt());
            System.out.println("当前在内存中：");
            for (int i = 10; i < Memory.pageNums; i++) {
                if (bitMap[i][0] == PID && bitMap[i][1] != -1) {
                    System.out.print(bitMap[i][1]+ " ");
                }
            }
            System.out.println();
        } else {
            System.out.println("当前在内存中：");
            for (int i = 10; i < Memory.pageNums; i++) {
                if (bitMap[i][0] == PID && bitMap[i][1] != -1) {
                    System.out.print(bitMap[i][1] + " ");
                }
            }
            System.out.println();
        }
        System.out.println();


        System.out.println("第4次");
        int result4 = memoryManager.Write(register1, logicalAddress4, bytes);
        if (result4 == MemoryManagement.PAGE_FAULT) {
            memoryManager.PageFaultProcess(register1, logicalAddress4, ByteBuffer.wrap(bytes).getInt());
            System.out.println("缺页,换出:" + ByteBuffer.wrap(bytes).getInt());
            System.out.println("当前在内存中：");
            for (int i = 10; i < Memory.pageNums; i++) {
                if (bitMap[i][0] == PID && bitMap[i][1] != -1) {
                    System.out.print(bitMap[i][1]+ " ");
                }
            }
            System.out.println();
        } else {
            System.out.println("当前在内存中：");
            for (int i = 10; i < Memory.pageNums; i++) {
                if (bitMap[i][0] == PID && bitMap[i][1] != -1) {
                    System.out.print(bitMap[i][1]+ " ");
                }
            }
            System.out.println();
        }
        System.out.println();


        System.out.println("第5次");
        int result5 = memoryManager.Write(register1, logicalAddress5, bytes);
        if (result5 == MemoryManagement.PAGE_FAULT) {
            memoryManager.PageFaultProcess(register1, logicalAddress5, ByteBuffer.wrap(bytes).getInt());
            System.out.println("缺页,换出:" + ByteBuffer.wrap(bytes).getInt());
            System.out.println("当前在内存中：");
            for (int i = 10; i < Memory.pageNums; i++) {
                if (bitMap[i][0] == PID && bitMap[i][1] != -1) {
                    System.out.print(bitMap[i][1] + " ");
                }
            }
            System.out.println();
        } else {
            System.out.println("当前在内存中：");
            for (int i = 10; i < Memory.pageNums; i++) {
                if (bitMap[i][0] == PID && bitMap[i][1] != -1) {
                    System.out.print(bitMap[i][1]+ " ");
                }
            }
            System.out.println();
        }
        System.out.println();


        System.out.println("第6次");
        int result6 = memoryManager.Write(register1, logicalAddress6, bytes);
        if (result6 == MemoryManagement.PAGE_FAULT) {
            memoryManager.PageFaultProcess(register1, logicalAddress6, ByteBuffer.wrap(bytes).getInt());
            System.out.println("缺页,换出:" + ByteBuffer.wrap(bytes).getInt());
            System.out.println("当前在内存中：");
            for (int i = 10; i < Memory.pageNums; i++) {
                if (bitMap[i][0] == PID && bitMap[i][1] != -1) {
                    System.out.print(bitMap[i][1] + " ");
                }
            }
            System.out.println();
        } else {
            System.out.println("当前在内存中：");
            for (int i = 10; i < Memory.pageNums; i++) {
                if (bitMap[i][0] == PID && bitMap[i][1] != -1) {
                    System.out.print(bitMap[i][1] + " ");
                }
            }
            System.out.println();
        }
        System.out.println();


        System.out.println("第7次");
        int result7 = memoryManager.Write(register1, logicalAddress7, bytes);
        if (result7 == MemoryManagement.PAGE_FAULT) {
            memoryManager.PageFaultProcess(register1, logicalAddress7, ByteBuffer.wrap(bytes).getInt());
            System.out.println("缺页,换出:" + ByteBuffer.wrap(bytes).getInt());
            System.out.println("当前在内存中：");
            for (int i = 10; i < Memory.pageNums; i++) {
                if (bitMap[i][0] == PID && bitMap[i][1] != -1) {
                    System.out.print(bitMap[i][1] + " ");
                }
            }
            System.out.println();
        } else {
            System.out.println("当前在内存中：");
            for (int i = 10; i < Memory.pageNums; i++) {
                if (bitMap[i][0] == PID && bitMap[i][1] != -1) {
                    System.out.print(bitMap[i][1] + " ");
                }
            }
            System.out.println();
        }
        System.out.println();


        System.out.println("第8次");
        int result8 = memoryManager.Write(register1, logicalAddress8, bytes);
        if (result8 == MemoryManagement.PAGE_FAULT) {
            memoryManager.PageFaultProcess(register1, logicalAddress8, ByteBuffer.wrap(bytes).getInt());
            System.out.println("缺页,换出:" + ByteBuffer.wrap(bytes).getInt());
            System.out.println("当前在内存中：");
            for (int i = 10; i < Memory.pageNums; i++) {
                if (bitMap[i][0] == PID && bitMap[i][1] != -1) {
                    System.out.print(bitMap[i][1] + " ");
                }
            }
            System.out.println();
        } else {
            System.out.println("当前在内存中：");
            for (int i = 10; i < Memory.pageNums; i++) {
                if (bitMap[i][0] == PID && bitMap[i][1] != -1) {
                    System.out.print(bitMap[i][1] + " ");
                }
            }
            System.out.println();
        }
        System.out.println();


        System.out.println("第9次");
        int result9 = memoryManager.Write(register1, logicalAddress9, bytes);
        if (result9 == MemoryManagement.PAGE_FAULT) {
            memoryManager.PageFaultProcess(register1, logicalAddress9, ByteBuffer.wrap(bytes).getInt());
            System.out.println("缺页,换出:" + ByteBuffer.wrap(bytes).getInt());
            System.out.println("当前在内存中：");
            for (int i = 10; i < Memory.pageNums; i++) {
                if (bitMap[i][0] == PID && bitMap[i][1] != -1) {
                    System.out.print(bitMap[i][1]+ " ");
                }
            }
            System.out.println();
        } else {
            System.out.println("当前在内存中：");
            for (int i = 10; i < Memory.pageNums; i++) {
                if (bitMap[i][0] == PID && bitMap[i][1] != -1) {
                    System.out.print(bitMap[i][1] + " ");
                }
            }
            System.out.println();
        }
        System.out.println();


        System.out.println("第10次");
        int result10 = memoryManager.Write(register1, logicalAddress10, bytes);
        if (result10 == MemoryManagement.PAGE_FAULT) {
            memoryManager.PageFaultProcess(register1, logicalAddress10, ByteBuffer.wrap(bytes).getInt());
            System.out.println("缺页,换出:" + ByteBuffer.wrap(bytes).getInt());
            System.out.println("当前在内存中：");
            for (int i = 10; i < Memory.pageNums; i++) {
                if (bitMap[i][0] == PID && bitMap[i][1] != -1) {
                    System.out.print(bitMap[i][1] + " ");
                }
            }
            System.out.println();
        } else {
            System.out.println("当前在内存中：");
            for (int i = 10; i < Memory.pageNums; i++) {
                if (bitMap[i][0] == PID && bitMap[i][1] != -1) {
                    System.out.print(bitMap[i][1] + " ");
                }
            }
            System.out.println();
        }
        System.out.println();

        System.out.println("第11次");
        int result11 = memoryManager.Write(register1, logicalAddress11, bytes);
        if (result11 == MemoryManagement.PAGE_FAULT) {
            memoryManager.PageFaultProcess(register1, logicalAddress11, ByteBuffer.wrap(bytes).getInt());
            System.out.println("缺页,换出:" + ByteBuffer.wrap(bytes).getInt());
            System.out.println("当前在内存中：");
            for (int i = 10; i < Memory.pageNums; i++) {
                if (bitMap[i][0] == PID && bitMap[i][1] != -1) {
                    System.out.print(bitMap[i][1] + " ");
                }
            }
            System.out.println();
        } else {
            System.out.println("当前在内存中：");
            for (int i = 10; i < Memory.pageNums; i++) {
                if (bitMap[i][0] == PID && bitMap[i][1] != -1) {
                    System.out.print(bitMap[i][1] + " ");
                }
            }
            System.out.println();
        }
        System.out.println();


        System.out.println("---------------------进程2-----------------------------");
        int PID2 = 54;
        // Act
        int register12 = memoryManager.Allocate(PID2, size);

        System.out.println("第1次");
        int result12 = memoryManager.Write(register12, logicalAddress1, bytes);
        if (result12 == MemoryManagement.PAGE_FAULT) {
            memoryManager.PageFaultProcess(register12, logicalAddress1, ByteBuffer.wrap(bytes).getInt());
            System.out.println("缺页,换出:" + ByteBuffer.wrap(bytes).getInt());
            System.out.println("当前在内存中：");
            for (int i = 10; i < Memory.pageNums; i++) {
                if (bitMap[i][0] == PID2 && bitMap[i][1] != -1) {
                    System.out.print(bitMap[i][1] + " ");
                }
            }
            System.out.println();
        } else {
            System.out.println("当前在内存中：");
            for (int i = 10; i < Memory.pageNums; i++) {
                if (bitMap[i][0] == PID2 && bitMap[i][1] != -1) {
                    System.out.print(bitMap[i][1] + " ");
                }
            }
            System.out.println();
        }
        System.out.println();


        System.out.println("第2次");
        int result22 = memoryManager.Write(register12, logicalAddress2, bytes);
        if (result22 == MemoryManagement.PAGE_FAULT) {
            memoryManager.PageFaultProcess(register12, logicalAddress2, ByteBuffer.wrap(bytes).getInt());
            System.out.println("缺页,换出:" + ByteBuffer.wrap(bytes).getInt());
            System.out.println("当前在内存中：");
            for (int i = 10; i < Memory.pageNums; i++) {
                if (bitMap[i][0] == PID2 && bitMap[i][1] != -1) {
                    System.out.print(bitMap[i][1]+ " ");
                }
            }
            System.out.println();
        } else {
            System.out.println("当前在内存中：");
            for (int i = 10; i < Memory.pageNums; i++) {
                if (bitMap[i][0] == PID2 && bitMap[i][1] != -1) {
                    System.out.print(bitMap[i][1]+ " ");
                }
            }
            System.out.println();
        }
        System.out.println();

        System.out.println("第3次");
        int result32 = memoryManager.Write(register12, logicalAddress3, bytes);
        if (result32 == MemoryManagement.PAGE_FAULT) {
            memoryManager.PageFaultProcess(register12, logicalAddress3, ByteBuffer.wrap(bytes).getInt());
            System.out.println("缺页,换出:" + ByteBuffer.wrap(bytes).getInt());
            System.out.println("当前在内存中：");
            for (int i = 10; i < Memory.pageNums; i++) {
                if (bitMap[i][0] == PID2 && bitMap[i][1] != -1) {
                    System.out.print(bitMap[i][1]+ " ");
                }
            }
            System.out.println();
        } else {
            System.out.println("当前在内存中：");
            for (int i = 10; i < Memory.pageNums; i++) {
                if (bitMap[i][0] == PID2 && bitMap[i][1] != -1) {
                    System.out.print(bitMap[i][1] + " ");
                }
            }
            System.out.println();
        }
        System.out.println();


        System.out.println("第4次");
        int result42 = memoryManager.Write(register12, logicalAddress4, bytes);
        if (result42 == MemoryManagement.PAGE_FAULT) {
            memoryManager.PageFaultProcess(register12, logicalAddress4, ByteBuffer.wrap(bytes).getInt());
            System.out.println("缺页,换出:" + ByteBuffer.wrap(bytes).getInt());
            System.out.println("当前在内存中：");
            for (int i = 10; i < Memory.pageNums; i++) {
                if (bitMap[i][0] == PID2 && bitMap[i][1] != -1) {
                    System.out.print(bitMap[i][1]+ " ");
                }
            }
            System.out.println();
        } else {
            System.out.println("当前在内存中：");
            for (int i = 10; i < Memory.pageNums; i++) {
                if (bitMap[i][0] == PID2 && bitMap[i][1] != -1) {
                    System.out.print(bitMap[i][1]+ " ");
                }
            }
            System.out.println();
        }
        System.out.println();


        System.out.println("第5次");
        int result52 = memoryManager.Write(register12, logicalAddress5, bytes);
        if (result52 == MemoryManagement.PAGE_FAULT) {
            memoryManager.PageFaultProcess(register12, logicalAddress5, ByteBuffer.wrap(bytes).getInt());
            System.out.println("缺页,换出:" + ByteBuffer.wrap(bytes).getInt());
            System.out.println("当前在内存中：");
            for (int i = 10; i < Memory.pageNums; i++) {
                if (bitMap[i][0] == PID2 && bitMap[i][1] != -1) {
                    System.out.print(bitMap[i][1] + " ");
                }
            }
            System.out.println();
        } else {
            System.out.println("当前在内存中：");
            for (int i = 10; i < Memory.pageNums; i++) {
                if (bitMap[i][0] == PID2 && bitMap[i][1] != -1) {
                    System.out.print(bitMap[i][1]+ " ");
                }
            }
            System.out.println();
        }
        System.out.println();


        System.out.println("第6次");
        int result62 = memoryManager.Write(register12, logicalAddress6, bytes);
        if (result62 == MemoryManagement.PAGE_FAULT) {
            memoryManager.PageFaultProcess(register12, logicalAddress6, ByteBuffer.wrap(bytes).getInt());
            System.out.println("缺页,换出:" + ByteBuffer.wrap(bytes).getInt());
            System.out.println("当前在内存中：");
            for (int i = 10; i < Memory.pageNums; i++) {
                if (bitMap[i][0] == PID2 && bitMap[i][1] != -1) {
                    System.out.print(bitMap[i][1] + " ");
                }
            }
            System.out.println();
        } else {
            System.out.println("当前在内存中：");
            for (int i = 10; i < Memory.pageNums; i++) {
                if (bitMap[i][0] == PID2 && bitMap[i][1] != -1) {
                    System.out.print(bitMap[i][1] + " ");
                }
            }
            System.out.println();
        }
        System.out.println();


        System.out.println("第7次");
        int result72 = memoryManager.Write(register12, logicalAddress7, bytes);
        if (result72 == MemoryManagement.PAGE_FAULT) {
            memoryManager.PageFaultProcess(register12, logicalAddress7, ByteBuffer.wrap(bytes).getInt());
            System.out.println("缺页,换出:" + ByteBuffer.wrap(bytes).getInt());
            System.out.println("当前在内存中：");
            for (int i = 10; i < Memory.pageNums; i++) {
                if (bitMap[i][0] == PID2 && bitMap[i][1] != -1) {
                    System.out.print(bitMap[i][1] + " ");
                }
            }
            System.out.println();
        } else {
            System.out.println("当前在内存中：");
            for (int i = 10; i < Memory.pageNums; i++) {
                if (bitMap[i][0] == PID2 && bitMap[i][1] != -1) {
                    System.out.print(bitMap[i][1] + " ");
                }
            }
            System.out.println();
        }
        System.out.println();


        System.out.println("第8次");
        int result82 = memoryManager.Write(register12, logicalAddress8, bytes);
        if (result82 == MemoryManagement.PAGE_FAULT) {
            memoryManager.PageFaultProcess(register12, logicalAddress8, ByteBuffer.wrap(bytes).getInt());
            System.out.println("缺页,换出:" + ByteBuffer.wrap(bytes).getInt());
            System.out.println("当前在内存中：");
            for (int i = 10; i < Memory.pageNums; i++) {
                if (bitMap[i][0] == PID2 && bitMap[i][1] != -1) {
                    System.out.print(bitMap[i][1] + " ");
                }
            }
            System.out.println();
        } else {
            System.out.println("当前在内存中：");
            for (int i = 10; i < Memory.pageNums; i++) {
                if (bitMap[i][0] == PID2 && bitMap[i][1] != -1) {
                    System.out.print(bitMap[i][1] + " ");
                }
            }
            System.out.println();
        }
        System.out.println();


        System.out.println("第9次");
        int result92 = memoryManager.Write(register12, logicalAddress9, bytes);
        if (result92 == MemoryManagement.PAGE_FAULT) {
            memoryManager.PageFaultProcess(register12, logicalAddress9, ByteBuffer.wrap(bytes).getInt());
            System.out.println("缺页,换出:" + ByteBuffer.wrap(bytes).getInt());
            System.out.println("当前在内存中：");
            for (int i = 10; i < Memory.pageNums; i++) {
                if (bitMap[i][0] == PID2 && bitMap[i][1] != -1) {
                    System.out.print(bitMap[i][1]+ " ");
                }
            }
            System.out.println();
        } else {
            System.out.println("当前在内存中：");
            for (int i = 10; i < Memory.pageNums; i++) {
                if (bitMap[i][0] == PID2 && bitMap[i][1] != -1) {
                    System.out.print(bitMap[i][1] + " ");
                }
            }
            System.out.println();
        }
        System.out.println();


        System.out.println("第10次");
        int result102 = memoryManager.Write(register12, logicalAddress10, bytes);
        if (result102 == MemoryManagement.PAGE_FAULT) {
            memoryManager.PageFaultProcess(register12, logicalAddress10, ByteBuffer.wrap(bytes).getInt());
            System.out.println("缺页,换出:" + ByteBuffer.wrap(bytes).getInt());
            System.out.println("当前在内存中：");
            for (int i = 10; i < Memory.pageNums; i++) {
                if (bitMap[i][0] == PID2 && bitMap[i][1] != -1) {
                    System.out.print(bitMap[i][1] + " ");
                }
            }
            System.out.println();
        } else {
            System.out.println("当前在内存中：");
            for (int i = 10; i < Memory.pageNums; i++) {
                if (bitMap[i][0] == PID2 && bitMap[i][1] != -1) {
                    System.out.print(bitMap[i][1] + " ");
                }
            }
            System.out.println();
        }
        System.out.println();

        System.out.println("第11次");
        int result112 = memoryManager.Write(register12, logicalAddress11, bytes);
        if (result112 == MemoryManagement.PAGE_FAULT) {
            memoryManager.PageFaultProcess(register12, logicalAddress11, ByteBuffer.wrap(bytes).getInt());
            System.out.println("缺页,换出:" + ByteBuffer.wrap(bytes).getInt());
            System.out.println("当前在内存中：");
            for (int i = 10; i < Memory.pageNums; i++) {
                if (bitMap[i][0] == PID2 && bitMap[i][1] != -1) {
                    System.out.print(bitMap[i][1] + " ");
                }
            }
            System.out.println();
        } else {
            System.out.println("当前在内存中：");
            for (int i = 10; i < Memory.pageNums; i++) {
                if (bitMap[i][0] == PID2 && bitMap[i][1] != -1) {
                    System.out.print(bitMap[i][1] + " ");
                }
            }
            System.out.println();
        }
        System.out.println();

        String result = memoryManager.getPageUsageBitmap();
        System.out.println(result);

    }




}

